package com.zhulanhua.blogs.controller;

import com.zhulanhua.blogs.Utils.Code;
import com.zhulanhua.blogs.Utils.DozerUtils;
import com.zhulanhua.blogs.Utils.Result;
import com.zhulanhua.blogs.controller.params.CommentParams;
import com.zhulanhua.blogs.domain.Comment;
import com.zhulanhua.blogs.domain.query.CommentQuery;
import com.zhulanhua.blogs.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 评论接口
 * */
@RestController
@RequestMapping("/api/comments")
@CrossOrigin
public class CommentController {

    @Autowired
    CommentService commentService;
    @Autowired
    DozerUtils dozerUtils;

    /**
     * 获取该文章的评论
     * @param id  文章id
     * */
    @GetMapping("/reply")
    public Result<List<Comment>> getOk(@RequestParam long id){
        return new Result(Code.OK, commentService.getOk(id));
    }

    /**
     * 自定义筛选评论
     * @param commentQuery
     * */
    @GetMapping("/query")
    public Result<List<Comment>> getQuery(@Validated CommentQuery commentQuery){
        return new Result(Code.OK, commentService.getQuery(commentQuery));
    }

    /**
     * 获取评论列表
     * @param type 评论类型，0为等待审核，1审核通过，2审核不通过，默认为0
     * */
    @GetMapping("/list")
    public Result<List<Comment>> getNO(@RequestParam(value = "type",defaultValue = "0") int type){
        return new Result(Code.OK, commentService.getNo(0));
    }

    /**
     * 发布评论
     * @param comment
     * */
    @PostMapping("/save")
    public Result save(@Validated @RequestBody CommentParams comment){
        commentService.save(dozerUtils.convert(comment,Comment.class));
        return Result.ok();
    }

    /**
     * 删除评论
     * @param id 评论id
     * */
    @DeleteMapping("/delete")
    public Result delete(@RequestParam long id){
        commentService.delete(id);
        return Result.ok();
    }

    /**
     * 评论审核通过
     * @param id 评论id
     * */
    @PutMapping("/ok")
    public Result updateOk(@RequestBody long id){
        commentService.update(id, 1);
        return Result.ok();
    }

    /**
     * 评论审核不通过
     * @param id 评论id
     * */
    @PutMapping("/no")
    public Result updateNo(@RequestBody long id){
        commentService.update(id, 2);
        return Result.ok();
    }

    /**
     * 评论点赞
     * @param id 评论id
     * */
    @PostMapping("/praise")
    public Result praiseSave(@RequestBody long id){
        commentService.praise(id, true);
        return Result.ok();
    }

    /**
     * 评论取消点赞
     * @param id 评论id
     * */
    @DeleteMapping("/praise")
    public Result praiseDelete(@RequestBody long id){
        commentService.praise(id, false);
        return Result.ok();
    }
}
